
package logindata;

import java.io.*;
import java.sql.*;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Login {

    public static int compare(String username, String password) {
        int value = 0;
        logindata.LoginData data = new logindata.LoginData();
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        String url = "jdbc:mysql://ucfilespace.uc.edu:3306/boroffja";
        String userSQL = "boroffja";
        String passwordSQL = "teamramrod";
        String userObtained;
        String passObtained;

        try {
            con = DriverManager.getConnection(url, userSQL, passwordSQL);
            pst = con.prepareStatement("SELECT * FROM UserData");
            rs = pst.executeQuery();
            while (rs.next()) {
                userObtained = rs.getString(1);
                if (userObtained.equals(username)) {
                    passObtained = rs.getString(2);
                    if (passObtained.equals(password)) {
                        data.username = username;
                        data.password = password;
                        data.permissions = rs.getInt(3);
                        switch (data.permissions) {
                            case 1:
                                new helpdesk.StudentInterface(username).setVisible(true);
                                break;
                            case 2:
                                new helpdesk.GradAssInterface(username).setVisible(true);
                                break;
                            case 3:
                                try {
                                    new helpdesk.GradAssCoordInterface(username).setVisible(true);
                                } catch (Exception e) {
                                    javax.swing.JOptionPane.showMessageDialog(null, "Unknown Error Occured", "Error", javax.swing.JOptionPane.ERROR_MESSAGE, null);
                                    throw (e);
                                }
                                break;
                        }
                        LoginFrame.setLoginVisible(false);
                        value = data.permissions;
                        break;
                    } else {
                        value = -2;//-2 = password error
                    }
                } else {
                    value = -3;//-3 = username error
                }
            }//scan through the database
        } catch (SQLException ex) {
            javax.swing.JOptionPane.showMessageDialog(LoginFrame.loginFrame, "An Error Occured Connecting to database", "Connection Error", javax.swing.JOptionPane.ERROR_MESSAGE, null);
            Logger lgr = Logger.getLogger(Login.class.getName());
            lgr.log(Level.SEVERE, ex.getMessage(), ex);
            value = -5;
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (SQLException ex) {
                javax.swing.JOptionPane.showMessageDialog(LoginFrame.loginFrame, "An Error Occured Connecting to database", "Connection Error", javax.swing.JOptionPane.ERROR_MESSAGE, null);
                Logger lgr = Logger.getLogger(Login.class.getName());
                lgr.log(Level.WARNING, ex.getMessage(), ex);
                value = -5;
            }
            return value;
        }
    }
}
